//
//  SCBaseViewController.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/23.
//

#import "SCBaseViewController.h"
#import "SCBasicKit.h"

@interface SCBaseViewController ()<UIViewControllerTransitioningDelegate>//此协议控制转场动画效果


@end

@implementation SCBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(_isCustomNavBar) {
        [self setupCustomNavBar];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(_isCustomNavBar) {
        [self.navigationController setNavigationBarHidden:true animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:false animated:YES];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)sc_present:(UIViewController *)vc {
    vc.transitioningDelegate = self;
    vc.modalPresentationStyle = 0;
    [self presentViewController:vc animated:true completion:nil];
}

#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    SCPushPopTransition *transition = [SCPushPopTransition new];
    transition.isPush = true;
    return transition;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    SCPushPopTransition *transition = [SCPushPopTransition new];
    transition.isPush = false;
    return transition;
}

#pragma mark - 导航栏设置，系统导航栏
- (void)setNaviAttributeTranslucent:(BOOL)translucent {
    
    if(_isCustomNavBar) { // 自定义导航栏
        _customNavBarHidden = translucent;
        if(_customNavBarHidden) {
            _navBar.hidden = _customNavBarHidden;
            [_navBar mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@0);
            }];
        } else {
            _navBar.hidden = _customNavBarHidden;
            [_navBar mas_updateConstraints:^(MASConstraintMaker *make) {
                if (@available(iOS 11.0, *)) {
                    make.height.equalTo(@(sc_navigation_height));
                } else {
                    make.height.equalTo(@64);
                }
            }];
        }
        return;
    }
    /// 系统导航栏
    UIImage *bgImage = [UIImage imageNamed:@"sc-navbg"];
    // 导航栏透明度
    self.navigationController.navigationBar.translucent = translucent;
    // 导航栏背景色
    self.navigationController.navigationBar.backgroundColor = UIColor.clearColor;
    // 导航栏背景图
    [self.navigationController.navigationBar setBackgroundImage:translucent ? [UIImage new] : bgImage forBarMetrics:UIBarMetricsDefault];
    // 去掉导航栏下横线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    if(@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground]; // 重置背景和阴影颜色
        appearance.backgroundColor = UIColor.clearColor;
        appearance.backgroundImage = translucent ? [UIImage new] : bgImage;
        appearance.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:sc_PingFangMediumFont(18)};
        appearance.shadowColor = UIColor.clearColor;
        appearance.shadowImage = [UIImage new];
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;// 带scroll滑动的页面
        self.navigationController.navigationBar.standardAppearance = appearance;// 常规页面
    }
}

- (void)addBackLeftItem {
    __weak typeof(self) weakSelf = self;
    [self addSCNaviItemType:@[@(SCNavBarItemTypeImage)] Content:@[@"sc-navback"] Position:SCNavBarItemLeft ClickItem:^(NSInteger index) {
        if(index == 0) {
            if(weakSelf.navigationController.viewControllers.count > 1) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }
        }
    }];
}

- (void)addBackLeftItemWithTitle:(NSString *)title {
    __weak typeof(self) weakSelf = self;
    [self addSCNaviItemType:@[@(SCNavBarItemTypeImage),@(SCNavBarItemTypeText)] Content:@[@"sc-navback",title] Position:SCNavBarItemLeft ClickItem:^(NSInteger index) {
        if(index == 0) {
            if(weakSelf.navigationController.viewControllers.count > 1) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            } else {
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }
        }
    }];
}

- (void)addSCNaviItemType:(NSArray *)types
                  Content:(NSArray *)contents
                 Position:(SCNavBarItemPosition)position
                ClickItem:(nonnull void (^)(NSInteger))clickBlock{
    
    if(types.count != contents.count) {
        NSLog(@"导航栏参数传入不正确");
        return;
    }
    if(types.count == 0 || contents.count == 0) {
        NSLog(@"导航栏参数传入不正确");
        return;
    }
    
    NSMutableArray *itemViews = @[].mutableCopy;
    for(NSInteger i = 0; i < types.count ; i++) {
        SCNavBarItemType type = (SCNavBarItemType)[types[i] intValue];
        UIView *itemView = nil;
        switch (type) {
            case SCNavBarItemTypeText:
                itemView = [self setupText:contents[i]];
                break;
            case SCNavBarItemTypeImage:
                itemView = [self setupImage:contents[i]];
                break;
            case SCNavBarItemTypeCustom:
                itemView = contents[i];
                break;
            default:
                break;
        }
        if(position == SCNavBarItemLeft) {
            itemView.tag = 2000+i;
        } else {
            itemView.tag = 3000+i;
        }
        /// 添加点击事件
        itemView.userInteractionEnabled = true;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:[self sc_selecorBlock:^(UIGestureRecognizer *sender) {
            UIView *view = sender.view;
            if(view.tag >= 3000) {
                NSInteger index = view.tag - 3000;
                clickBlock(index);
            } else {
                NSInteger index = view.tag - 2000;
                clickBlock(index);
            }
        }]];
        [itemView addGestureRecognizer:tap];
        UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:itemView];
        [itemViews addObject:buttonItem];
    }
    
    if(_isCustomNavBar) {
        [self setupItemViews:itemViews position:position];
    } else {
        if(position == SCNavBarItemLeft) {
            self.navigationItem.leftBarButtonItems = itemViews;
        } else {
            self.navigationItem.rightBarButtonItems = itemViews;
        }
    }
}

- (void)setupItemViews:(NSArray*)items position:(SCNavBarItemPosition)position {
    UIView *last = nil;
    for(NSInteger i = 0; i < items.count; i ++) {
        UIView *itemView = ((UIBarButtonItem*)items[i]).customView;
        [_navBar addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if(position==SCNavBarItemLeft) {
                if(last) {
                    make.left.equalTo(last.mas_right).offset(8);
                } else {
                    make.left.equalTo(@12);
                }
            } else {
                if(last) {
                    make.right.equalTo(last.mas_left).offset(-8);
                } else {
                    make.right.equalTo(@-12);
                }
            }
            make.bottom.equalTo(@-12);
        }];
        last = itemView;
    }
}

- (UIView *)setupText:(NSString*)text {
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = UIColor.whiteColor;
    label.text = text;
    
    UIView *itemView = [[UIView alloc] init];
    [itemView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(@0);
        make.width.lessThanOrEqualTo(@(0.6 * sc_screen_width));
    }];
    
    return itemView;
}

- (UIView *)setupImage:(NSString*)image {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    UIView *itemView = [[UIView alloc] init];
    [itemView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(@0);
        make.left.equalTo(@6);
        make.right.equalTo(@-6);
    }];
    return itemView;
}

#pragma mark - 导航栏设置，自定义导航栏
- (void)setupCustomNavBar {
    
    _isCustomNavBar = true;
        
    UIImage *bgImage = [UIImage imageNamed:@"sc-navbg"];
    UIImageView *navImageView = [[UIImageView alloc] init];
    _navBar = navImageView;
    navImageView.userInteractionEnabled = true;
    navImageView.image = bgImage;
    [self.view addSubview:navImageView];
    [navImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@(sc_navigation_height));
    }];
}

@end
